package com.burgeon.backend.EnterpriseWeChat.rest.vip;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class queryVipWarning extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public queryVipWarning(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<Vip> getData(String warningType, String storeId, int page) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(warningType, storeId,page), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Vip> vipList = new ArrayList<>();

        if(jsonArray.length()>0){
            for (int i = 0; i < jsonArray.length(); i++) {
                Vip vip = new Vip();
                vip.setVipName(jsonArray.getJSONArray(i).getString(0));
                vip.setCardNo(jsonArray.getJSONArray(i).getString(1));
                vip.setLogoImg(jsonArray.getJSONArray(i).getString(2));
                vip.setOpenid(jsonArray.getJSONArray(i).getString(3));
                vip.setUDType(jsonArray.getJSONArray(i).getString(4));
                vip.setDescription(jsonArray.getJSONArray(i).getString(5));
                vip.setId(jsonArray.getJSONArray(i).getString(6));
                vip.setRecentDate(jsonArray.getJSONArray(i).getString(7));
                vip.setLastDate(jsonArray.getJSONArray(i).getString(8));
                vip.setPhone(jsonArray.getJSONArray(i).getString(9));
                vip.setEmpId(jsonArray.getJSONArray(i).getString(10));
                vip.setSex(jsonArray.getJSONArray(i).getString(11));
                vip.setExternalUserId(jsonArray.getJSONArray(i).getString(12));
                vipList.add(vip);
            }
        }
//        System.err.println(vipList.size());
        return vipList;
    }

    private MultiValueMap Data(String warningType,String storeId,int page) {
        System.err.println(warningType);
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'C_VIP_ID;VIPNAME'");
        colums.add("'C_VIP_ID;CARDNO'");
        colums.add("'C_VIP_ID;LOGO_URL'");//OPENID2
        colums.add("'C_VIP_ID;OPENID2'");//OPENID2
        colums.add("UD_TYPE");
        colums.add("DESCRIPTION");
        colums.add("C_VIP_ID");
        colums.add("'C_VIP_ID;RECENTDATE'");//最近邀约日
        colums.add("'C_VIP_ID;FA_VIPACC_ID;LASTDATE'");//最近消费日
        colums.add("'C_VIP_ID;MOBIL'");//手机号
        colums.add("'C_VIP_ID;HR_EMPLOYEE_ID1'");//专属导购

        colums.add("'C_VIP_ID;SEX'");//OPENID2
        colums.add("'C_VIP_ID;EXTERNALUSERID'");//企业微信我的顾客标识


        params.put("columns", colums);
        params.put("table", "RFIM_VIP_UD");//表名
        params.put("range",RANGE);
//        params.put("count","true");
        int r = Integer.valueOf(RANGE);
        params.put("start",String.valueOf((page-1)*r));
//
//        HashMap orderby = new HashMap();
//        orderby.put("column","BILLDATE");
//        orderby.put("asc","false");
//
//        ArrayList list = new ArrayList();
//        list.add(orderby);
//        params.put("orderby",list);


        HashMap paramss = new HashMap();

        if (!warningType.equals("0")) {
            HashMap expr1 = new HashMap();
            expr1.put("column", "'C_STORE_ID'");
            expr1.put("condition", "'"+storeId+"'");
            HashMap expr2 = new HashMap();
            String ac = null;
            switch (warningType){
                case "1":
                    ac =  "'从D到A'";
                    expr2.put("column", "'DESCRIPTION'");
                    expr2.put("condition", ac);
                    break;
                case "2":
                     ac =  "'从C到A'";
                    expr2.put("column", "'DESCRIPTION'");
                    expr2.put("condition", ac);

                    break;
                case "3":
                    ac =  "'从A到D'";
                    expr2.put("column", "'DESCRIPTION'");
                    expr2.put("condition", ac);
                    break;
                case "4":
                    ac =  "'从A到C'";
                    expr2.put("column", "'DESCRIPTION'");
                    expr2.put("condition", ac);
                    break;
            }
            paramss.put("combine", "and");
            paramss.put("expr1", expr1);
            paramss.put("expr2", expr2);
            params.put("params", paramss);

            }else {
//            HashMap expr1 = new HashMap();
            paramss.put("column", "'C_STORE_ID'");
            paramss.put("condition", "'"+storeId+"'");
            params.put("params", paramss);
        }


//        params.put("params", paramss);
        System.err.println(params);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        queryVipWarning g = new queryVipWarning("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {//3125
            System.err.println(g.getData("0","1515",1));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
